//
//  ZCItemDetailVC.m
//  zctx
//
//  Created by 郭亚京 on 15/10/12.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCItemDetailVC.h"

@interface ZCItemDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *viewHeader;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) ZCItemModel *detailModel;

@end

@implementation ZCItemDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    [self setNavigationItems];
    [self.navigationItem setTitle:@"检测项目详情"];
    
    NSLog(@"mode.name = %@",self.model.name);
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0,0));
    }];
    
    [self requestMainList];
}

#pragma mark - requestMain

- (void)requestMainList {
    [NetworkManager requestFoundingGetPackageWithId:self.model.id success:^(ZCItemModel *model) {
        self.detailModel = model;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
}
-(NSAttributedString *)filterHTML:(NSString *)html
{
   NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    return attrStr;
}

#pragma mark - button

- (void)setNavigationItems {
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

- (void)backButtonPressed {
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableView DataSource & Delegate Methodsd

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
        cell.textLabel.numberOfLines = 0;
    }
    
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor grayColor];
    if (indexPath.section == 0) {
        cell.textLabel.text =self.detailModel.content;
    } else {
        cell.textLabel.text =self.detailModel.information;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = nil;
    if (indexPath.section == 0) {
        text =self.detailModel.content;
    } else {
        text =self.detailModel.information;
    }
    
    NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    return [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-30, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height+30;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    [view setBackgroundColor:[UIColor whiteColor]];
    UILabel *labelTitle = [[UILabel alloc] init];
    [labelTitle setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.centerY);
        make.height.equalTo(view.height);
        make.left.mas_equalTo(@(20));
        make.width.equalTo(view.width).with.offset(-40);
    }];
    labelTitle.textColor = [UIColor colorWithHexString:@"#333333"];
    if (section == 0) {
        //基本信息
        [labelTitle setText:@"检测内容"];
    } else {
        //检测信息
        [labelTitle setText:@"相关知识"];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - instance 

- (UIView *)viewHeader {
    if (!_viewHeader) {
        _viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        UILabel *labelTitle = [[UILabel alloc] init];
        [labelTitle setTextAlignment:NSTextAlignmentCenter];
        [labelTitle setBackgroundColor:[UIColor whiteColor]];
        [_viewHeader addSubview:labelTitle];
        [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(@(0));
            make.height.mas_equalTo(@(40));
            make.left.mas_equalTo(@(0));
            make.width.mas_equalTo(@(SCREEN_WIDTH));
        }];
        labelTitle.textColor = [UIColor colorWithHexString:@"03ab9e"];
        [labelTitle setText:self.model.name];
    }
    return _viewHeader;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.tableHeaderView = self.viewHeader;
    }
    return _tableView;
}


@end
